package com.saltedfish.tool.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

import com.saltedfish.tool.App;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SharePreferceUtils {

    private static SharedPreferences preferences;
    private static String FILENAME = "salted_fish";

    private static SharedPreferences getInstance() {
        if (preferences == null) {
            preferences = App.getInstance().getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        }
        return preferences;
    }

    /**
     * 存储字符串数据类型
     */
    public static void setString(String key, String value) {
        Editor editor = getInstance().edit();
        editor.putString(key, value);
        editor.apply();
        editor.commit();
    }

    /**
     * 返回String类型数据，默认是“”；
     */
    public static String getString(String key) {
        return getInstance().getString(key, "");
    }
    public static String getString(String key,String def) {
        return getInstance().getString(key, def);
    }

    /**
     * 存储boolean数据类型
     */
    public static void saveBoolean(String key, boolean value) {
        Editor editor = getInstance().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    /**
     * 返回boolean类型数据，默认是true；
     */
    public static boolean getBoolean(String key) {
        return getInstance().getBoolean(key, false);
    }

    /**
     * 存储int数据类型
     */
    public static void saveInt(String key, int value) {
        Editor editor = getInstance().edit();
        editor.putInt(key, value);
        editor.apply();
    }

    /**
     * 返回int类型数据，默认是true；
     */
    public static int getInt(String key) {
        return getInstance().getInt(key, 0);
    }

    /**
     * 存储float数据类型
     */
    public static void saveFloat(String key, float value) {
        Editor editor = getInstance().edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    /**
     * 返回float类型数据，默认是true；
     */
    public static float getFloat(String key) {
        return getInstance().getFloat(key, 0);
    }

    /**
     * 存储long数据类型
     */
    public static void saveLong(String key, long value) {
        Editor editor = getInstance().edit();
        editor.putLong(key, value);
        editor.apply();
    }

    /**
     * 返回long类型数据，默认是true；
     */
    public static long getLong(String key) {
        return getInstance().getLong(key, 0);
    }


    /**
     * 储存复杂的数据对象字段
     */
    public static <T> boolean saveObjToShareBase64(String key, T t) {
        return saveObjToShareBase64(FILENAME, key, t);
    }

    /**
     * 储存复杂的数据对象字段
     */
    public static <T> boolean saveObjToShareBase64(String name, String key, T t) {
        try {
            // 存储
            Editor editor = getInstance().edit();
            if (t == null) {
                editor.putString(key, "");
                editor.apply();
                return true;
            }
            ByteArrayOutputStream toByte = new ByteArrayOutputStream();
            ObjectOutputStream oos;
            oos = new ObjectOutputStream(toByte);
            oos.writeObject(t);
            // 对byte[]进行Base64编码
            String payCityMapBase64 = new String(Base64.encode(toByte.toByteArray(), Base64.DEFAULT));
            editor.putString(key, payCityMapBase64);
            editor.commit();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 得到复杂的数据对象
     */
    public static <T> T getObjFromShareBase64(String key) {
        return getObjFromShareBase64(FILENAME, key);
    }

    /**
     * 得到复杂的数据对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T getObjFromShareBase64(String name, String key) {
        try {
            String payCityMapBase64 = getInstance().getString(key, "");
            if (payCityMapBase64.length() == 0) {
                return null;
            }
            byte[] base64Bytes = Base64.decode(payCityMapBase64, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
            ObjectInputStream ois = new ObjectInputStream(bais);
            return (T) ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getUserToken(Context context) {
        return getInstance().getString("token", "");
    }
    public static String getUser_level(Context context) {
        return getInstance().getString("user_level", "0");
    }



    public static String getGenders(Context context) {
        return getInstance().getString("gender", "");
    }

    public static void setGenders(Context context, String gender) {
        Editor editor = getInstance().edit();
        editor.putString("gender", gender);
        editor.apply();
    }

    public static String getBirthday(Context context) {
        return getInstance().getString("birthday", "");
    }

    public static void setBirthday(Context context, String birthday) {
        Editor editor = getInstance().edit();
        editor.putString("birthday", birthday);
        editor.apply();
    }


    public static String getProvice(Context context) {
        return getInstance().getString("provice", "");
    }

    public static void setProvice(Context context, String provice) {
        Editor editor = getInstance().edit();
        editor.putString("provice", provice);
        editor.apply();
    }

    public static String getCity(Context context) {
        return getInstance().getString("city", "");
    }

    public static void setCity(Context context, String city) {
        Editor editor = getInstance().edit();
        editor.putString("city", city);
        editor.apply();
    }

    public static String getArea(Context context) {
        return getInstance().getString("area", "");
    }

    public static void setArea(Context context, String area) {
        Editor editor = getInstance().edit();
        editor.putString("area", area);
        editor.apply();
    }





    public static String getUmengPushValue(String type) {
        return getInstance().getString(type, "");
    }

    public static void setUmengPushValue(String key, String value) {
        Editor editor = getInstance().edit();
        editor.putString(key, value);
        editor.apply();
    }


    public static String getAppIconCache() {
        return getInstance().getString("Appicon", "");
    }
}
